﻿@using KC.Web.Admin.Constants
@using KC.MVC.Core.Constants
@using Microsoft.AspNetCore.Authorization
@inject IAuthorizationService AuthorizationService
@{
    ViewData["Title"] = "服务总线池管理";
    Layout = "~/Views/Shared/_ListLayout.cshtml";
}

<div id="toolbar">
    <div>
        @*服务总线池管理-保存服务总线连接*@
        @if ((await AuthorizationService.AuthorizeAsync(User, "0AB446BD-F380-43EB-B920-6CAE1A8A6AC0")).Succeeded)
        {
            <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-plus" onclick="addServiceBusPool()">添加</a>
        }
        @*服务总线池管理-保存服务总线连接*@
        @if ((await AuthorizationService.AuthorizeAsync(User, "0AB446BD-F380-43EB-B920-6CAE1A8A6AC0")).Succeeded)
        {
            <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-pencil" onclick="editServiceBusPool()">编辑</a>
        }
        @*服务总线池管理-删除服务总线连接*@
        @if ((await AuthorizationService.AuthorizeAsync(User, "A900D687-435B-4748-BF9F-A4D809CAB3E9")).Succeeded)
        {
            <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="fa fa-trash" onclick="removeServiceBusPool()">删除</a>
        }
    </div>

    <div>
        <div id="divSearchMenu" class="easyui-menu" style="width:120px">
            <div data-options="name:'Server',selected:true">访问名称</div>
        </div>
        <input id="txtSearchbox" class="easyui-searchbox" style="width:300px;height: 26px;" />
    </div>
</div>
<table id="datagrid"></table>
@section scripts
{
    <script type="text/javascript">
        $(function () {
            InitDataGrid();
        });
        var loadDataUrl = '@Url.Action(ActionName.ServiceBusPool.LoadServiceBusPoolList, ControllerName.ServiceBusPool)';
        function InitDataGrid() {
            var $dataGrid = $('#datagrid').datagrid({
                url: loadDataUrl,
                method: 'get',
                idField: 'ServiceBusPoolId',
                striped: true,
                fitColumns: true,
                rownumbers: true,
                singleSelect: true,
                pagination: true,
                nowrap: false,
                fit: true,
                pageSize: 10,
                pageList: [10, 20, 50, 100],
                showFooter: false,
                toolbar: "#toolbar",
                //toolbar: [{ text: document.getElementById('toolbar') }],
                columns: [[
                    { field: 'ServiceBusPoolId', title: 'Id', width: 30, align: 'left' },
                    { field: 'CloudTypeString', title: '云服务', width: 50, align: 'left' },
                    { field: 'DistributedMessageTypeString', title: '类型',width:50,align:'left' },
                        { field: 'Endpoint', title: '访问地址', width: 200, align: 'left' },
                        { field: 'AccessName', title: '访问名称', width: 100, align: 'left' },
                        { field: 'PasswordExpiredTime', title: '密码过期时间', formatter: Common.DateFormatter, width: 100, align: 'left' },
                        { field: 'TenantCount', title: '租户个数', width: 50, align: 'left', editor: { type: 'numberbox', options: { min: 20, precision: 0 } } }
                ]],
                onBeforeLoad: function (param) {
                    /*var bId = $("#txtBId").val();
                var AllSearchKey = $("#txtAllSearchKey").val();
                param.bId = bId;
                param.AllSearchKey = AllSearchKey;*/
                },
                onLoadSuccess: function (data) {
                },
                onLoadError: function () {
                }
            });

            $('#txtSearchbox').searchbox({
                menu: '#divSearchMenu',
                prompt: '请输入查询值',
                searcher: searchData
            });
        }

        var getFormUrl = '@Url.Action(ActionName.ServiceBusPool.GetServiceBusPoolForm, ControllerName.ServiceBusPool)';
        var saveFormUrl = '@Url.Action(ActionName.ServiceBusPool.SaveserviceBusPoolForm, ControllerName.ServiceBusPool)';
        function opentForm(id) {
            var dialog = $.easyui.showDialog({
                title: "保存数据",
                width: 420,
                height: 350,
                href: getFormUrl + "?id=" + id,
                modal: true,
                topMost: false,
                enableHeaderContextMenu: false,
                enableApplyButton: false,
                onSave: function (d) {
                    var validate = d.form("enableValidation").form("validate");
                    if (validate) {
                        $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                        $.ajax({
                            async: true,
                            type: "post",
                            dataType: "json",
                            url: saveFormUrl,
                            data: AddAntiForgeryToken(d.form("getData")),
                            success: function (data) {
                                if (data.success) {
                                    d.window('close');
                                    if (data.Result) {
                                        $('#datagrid').datagrid('reload');
                                        $.messager.showInfoCenter('系统提示', '保存数据成功。', 1000);
                                    } else {
                                        $.messager.showErrorCenter('错误消息', '保存数据失败。', 1000);
                                    }
                                } else {
                                    $.messager.showErrorCenter('错误消息', data.message, 1000);
                                }
                            },
                            complete: function () {
                                $.easyui.loaded();
                            }
                        });
                        return false;
                    } else {
                        return false;
                    }
                },
                onLoad: function () {
                    var f = $(this), ret = $.fn.dialog.defaults.onLoad();
                    f.form("disableValidation").form("enableValidation");
                    return ret;
                }
            });
        }
        function searchData(value, name) {
            $('#datagrid').datagrid('unselectAll');
            $('#datagrid').datagrid('load', { "searchKey": name, "searchValue": value });
        }

        function addServiceBusPool() {
            opentForm(0);
        }
        function editServiceBusPool() {
            var row = $('#datagrid').datagrid('getSelected');
            if (row) {

                opentForm(row.ServiceBusPoolId);
            } else {
                $.messager.showErrorCenter('系统提示', '请选择需要编辑的数据！', 2000);
            }
        }

        var removeUrl = '@Url.Action(ActionName.ServiceBusPool.DeleteServiceBusPool, ControllerName.ServiceBusPool)';
        function removeServiceBusPool() {
            var row = $('#datagrid').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否确定删除该条记录?', function (r) {
                    if (r) {
                        $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                        $.post(removeUrl, { id: row.ServiceBusPoolId }, function (data) {
                            $.easyui.loaded();
                            if (data.success) {
                                if (data.Result) {
                                    $('#datagrid').datagrid('reload');
                                    $.messager.showInfoCenter('系统提示', '删除数据成功。', 1000);
                                } else {
                                    $.messager.showErrorCenter('错误消息', '删除数据失败。', 1000);
                                }
                            } else {
                                $.data.showErrorCenter('错误消息', data.message, 1000);
                            }
                        }, 'json');
                    }
                });
            } else {
                $.messager.showErrorCenter('系统提示', '请选择需要删除的数据！', 2000);
            }
        }

        var getPasswordHashUrl = '@Url.Action(ActionName.Tenant.GeneratePasswordHash, ControllerName.Tenant)';
        function generatePasswordHash() {
            //debugger;
            var password = $('#AccessKeyPassword').val();
            if (password == '') {
                $.messager.showErrorCenter('错误消息', "访问密钥不能为空", 1000);
                return;
            }
            $.messager.confirm('确认', '是否根据密码:（' + password + '）进行加密，生成加密后的密码?', function (r) {
                if (r) {
                    $.easyui.loading({ msg: '正在保存数据，请稍等...' });
                    $.post(getPasswordHashUrl, { password: password }, function (result) {
                        $.easyui.loaded();
                        if (result.success) {
                            $('#AccessKeyPasswordHash').val(result.message);
                        } else {
                            $.messager.showErrorCenter('错误消息', result.message, 1000);
                        }
                    }, 'json');
                }
            });
        }

       @*var testConnectionUrl = '@Url.Action(ActionName.Tenant.TestServiceBusPoolConnection, ControllerName.Tenant)';
        function testConnection() {
            //debugger;
            var cloudType = $('#CloudType').val();
            if (cloudType == '') {
                $.messager.showErrorCenter('错误消息', "云类型不能为空", 1000);
                return;
            }
            var endpoint = $('#Endpoint').val();
            if (endpoint == '') {
                $.messager.showErrorCenter('错误消息', "服务器地址不能为空", 1000);
                return;
            }
            var accessName = $('#AccessName').val();
            if (accessName == '') {
                $.messager.showErrorCenter('错误消息', "服务器AccessName不能为空", 1000);
                return;
            }
            var accessKeyHash = $('#AccessKeyPasswordHash').val();
            if (accessKeyHash == '') {
                $.messager.showErrorCenter('错误消息', "服务器AccessKey不能为空", 1000);
                return;
            }
            $.easyui.loading({ msg: '正在保存数据，请稍等...' });
            $.post(testConnectionUrl,
                {
                    CloudType: cloudType,
                    Endpoint: endpoint,
                    AccessName: accessName,
                    AccessKeyPasswordHash: accessKeyHash,
                },
                function (data) {
                    $.easyui.loaded();
                    if (data.success) {
                        if (data.Result == '') {
                            $.messager.showInfoCenter('系统提示', '测试消息队列连接成功。', 1000);
                        } else {
                            $.messager.showErrorCenter('错误消息', data.Result, 3000);
                        }
                    } else {
                        $.messager.showErrorCenter('错误消息', data.message, 1000);
                    }
                }, 'json');
        }*@
    </script>
}



